<template>
	<div class="bg">
		<div class="zzc" v-if="showzzc" @click="close"></div>
		<div class="container">
			<div class="search_box">
				<div class="select_box" @click.stop="options_show=!options_show;showzzc=!showzzc">
					<div class="form-control" id="select" style="width: 120px;">
						<div :style="ind=='all'?'color:#999':''">
							{{ind!='all'?types[ind].name:'查询方式'}}
						</div>
						<div v-if="ind!='all'" @click.stop="closeall">
							<img src="@assets/images/close.png" alt="">
						</div>
						<img src="@assets/images/down.png" alt="">
					</div>
					<div class="options" v-if="options_show">
						<div class="option" @click.stop="selectit(index)" v-for="item,index in types">{{item.name}}
						</div>
					</div>
				</div>

				<el-date-picker v-if="ind==0||ind==2 || ind==1" prefix-icon='none' ref="year"
					style="width: 120px;margin-left: 10px;" v-model="info.year" type="year" @input="changeyear"
					value-format="yyyy" placeholder="年份">
				</el-date-picker>



				<div class="select_box" v-if="ind==1" @click.stop="options_show3=!options_show3;showzzc=!showzzc">
					<div class="form-control" id="select" style="width: 120px;">
						<div :style="ind3=='all'?'color:#999;width:40px':'width:40px'">
							{{ind3!='all'?month[ind3]:'月'}}
						</div>
						<div v-if="ind3!='all'" @click.stop="info.month='',ind3='all'">
							<img src="@assets/images/close.png" alt="">
						</div>

						<img src="@assets/images/down.png" alt="">
					</div>
					<div class="options" v-if="options_show3">
						<div class="option" @click.stop="selectit3(index)" v-for="item,index in month">{{item}}</div>
					</div>
				</div>


				<div class="sea_btn sear_btns" @click="getlist();getnums()">
					<img src="@assets/images/search.png" alt="">
					查询
				</div>

			</div>


			<div class="box">
				<el-row :gutter="20">
					<el-col :span="6">
						<div class="count">
							<div class="title_top">巡检总数</div>
							<div class="title_num">{{nums.count}}</div>
						</div>
					</el-col>
					<el-col :span="6">
						<div class="count">
							<div class="title_top">巡检单元数</div>
							<div class="title_num">{{nums.unitCount}}</div>
						</div>
					</el-col>
					<el-col :span="6">
						<div class="count">
							<div class="title_top">巡检资产数</div>
							<div class="title_num">{{nums.zcCount}}</div>
						</div>
					</el-col>
					<el-col :span="6">
						<div class="count">
							<div class="title_top">现存异常数</div>
							<div class="title_num">{{nums.exception}}</div>
						</div>
					</el-col>
				</el-row>
				<div class="box_top">
					<div class="box_top_left" ref="container"></div>
				</div>

				<div class="table">
					<div class="table_top">
						<div class="table_title">按工程师统计</div>
						<div class="top_btns" v-if="$permissions.indexOf(242)  != -1">

							<div class="print" @click="tabshow=true">打印</div>
						</div>
					</div>
					<el-table :data="tableData" style="width: 100%" :default-sort="{prop: 'date', order: 'descending'}">
						<el-table-column prop="name" label="姓名">
						</el-table-column>
						<el-table-column prop="count" label="巡检总数" sortable>
						</el-table-column>
						<el-table-column prop="unitCount" label="巡检单元数" sortable></el-table-column>
						<el-table-column prop="zcCount" label="巡检资产数" sortable></el-table-column>
						<el-table-column prop="exception" label="巡检异常数" sortable></el-table-column>
					</el-table>
				</div>
			</div>
			<div class="blackzzc" v-if="tabshow">
				<div class="tablealert" id="printDiv">
					<div class="top_btns">
						<div class="print" @click="print">确认打印</div>
						<div class="export" @click="tabshow=false">关闭</div>
					</div>
					<vueEasyPrint ref="easyPrint" tableShow>
						<div class="altabletitle">{{table_title}}</div>
						<table border="1" cellspacing='0'
							style="width:1000px;margin: 0 auto;text-align: center;color: #000;">
							<tr>
								<td>姓名</td>
								<td>巡检总数</td>
								<td>巡检单元数</td>
								<td>巡检资产数</td>
								<td>巡检异常数</td>
							</tr>
							<tr v-for="item in tableData">
								<td>{{item.name}}</td>
								<td>{{item.count}}</td>
								<td>{{item.unitCount}}</td>
								<td>{{item.zcCount}}</td>
								<td>{{item.exception}}</td>
							</tr>
						</table>
					</vueEasyPrint>
				</div>
			</div>

		</div>
	</div>
</template>
<script>
	import $ from 'jquery'
	import vueEasyPrint from "vue-easy-print"

	export default {
		name: "",
		components: {
			vueEasyPrint,
		},
		data() {
			return {
				tabshow: false,
				table_title: "巡检数量按工程师统计",
				types: [{
						name: '按年份',
						id: 1
					},
					{
						name: '按月份',
						id: 2
					},
				],
				month: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
				ind: 1,
				ind2: 'all',
				ind3: 'all',
				showzzc: false,
				options_show: false,
				options_show2: false,
				options_show3: false,
				phraseList: [],

				tableData: [],
				startDate: "",
				endDate: "",
				info: {},
				nums: {},
				ax: [],
				ay: []
			}
		},
		mounted() {
			this.timeFormate();

			this.getlist();
			this.getnums();

		},

		methods: {
			changeyear(e){
				console.log(e)
				this.info.year = e
				this.$forceUpdate()
			},
			getnums(){
				this.showloading = true
				var that = this
				var param = this.info
				this.$http.post(this.$url + '/xj/statistics/byYearAndMonth', this.$qs.stringify(param))
					.then(function(response) {
						if (response.data.code == '1410') {
							that.$Toast(response.data.msg);
							sessionStorage.setItem('token', '')
							that.$router.push('/login')
							return
						}
						that.nums = response.data.data
						that.showloading = false
					})
					.catch(function(error) {
						console.log(error);
							
						that.$Toast('网络拥堵，刷新一下');
					});
			},
			print() {
				this.$refs.easyPrint.print()
			},

			closeall() {
				this.info.searchType = ''
				this.info.year = ''
				this.info.quarter = ''
				this.info.month = ''
				this.info.startDate = ''
				this.info.endDate = ''
				this.ind = 'all'
			},
			//显示当前时间（年月日时分秒）
			timeFormate(timeStamp) {
				if (timeStamp) {
					let year = new Date(timeStamp).getFullYear();
					let month = new Date(timeStamp).getMonth() + 1
					let date = new Date(timeStamp).getDate()
					return year + "-" + month + "-" + date;
				} else {
					timeStamp = new Date()
					let year = new Date(timeStamp).getFullYear();
					let month = new Date(timeStamp).getMonth() + 1
					// console.log(month)
					this.info.year = year.toString()
					this.ind3 = month - 1
					this.info.month = month
				}
			},
			getlist() {

				if (this.info.year == '' && this.info.searchType == 1) {
					this.$Toast('请选择年份');
					return
				}
				if (this.info.searchType == 1) {
					this.info.month = ''
				}

				if (this.info.searchType == 2) {
					if (this.info.year == '' || this.info.month == '' || this.info.year == null) {
						this.$Toast('请选择年份和月份');
						return
					}
					// return
				}

				var that = this
				var param = this.info
				this.$http.post(this.$url + '/xj/statistics/tableByYearAndMonth', this.$qs.stringify(param))
					.then(function(response) {
						if (response.data.code == '1410') {
							that.$Toast(response.data.msg);
							sessionStorage.setItem('token', '')
							that.$router.push('/login')
							return
						}
						that.ax = []
						that.ay = []
						that.tableData = response.data.data
						for (var i = 0; i < that.tableData.length; i++) {
							// that.tableData[i].processTime = that.tableData[i].processTime ? that.tableData[i]
							// 	.processTime : 0
							that.ax[i] = that.tableData[i].name
							that.ay[i] = that.tableData[i].count
						}

						that.getbar();
					})
					.catch(function(error) {
						console.log(error);
					});
			},
			formatter(row, column) {
				return row.address;
			},
			getbar() {
				var dom = this.$refs.container
				var myChart = this.$echarts.init(dom, null, {
					renderer: 'canvas',
					useDirtyRect: false
				});
				var app = {};

				var option;

				option = {
					title: {
						text: "按工程师统计",
						top: 0,
						textStyle: {
							fontSize: 16,
							color: "#626262",
						},
						// padding: 17,
						left: 14
					},
					tooltip: {
						trigger: 'axis',
						axisPointer: {
							type: 'shadow'
						}
					},
					grid: {
						top: 70,
						left: 20,
						right: 20,
						bottom: 9,
						containLabel: true
					},
					xAxis: [{
						type: 'category',
						data: this.ax,
						axisLabel: {
							interval: 0,
							rotate: 30
						}
					}],
					yAxis: [{
						type: 'value',
						name: '巡检数量',
						min: 0,
						nameRotate: this.ay.length == 0 ? 1 : 0,
						// max: 5,
						minInterval: 1,
						axisLabel: {
							formatter: '{value}次'
						}

					}],
					series: [{
						data: this.ay,
						type: 'bar',
						barMaxWidth: 70,
						barWidth: 40,
						itemStyle: {
							color: '#527cff'
						},
					}]
				};

				if (option && typeof option === 'object') {
					myChart.setOption(option);
				}

				window.addEventListener('resize', myChart.resize);
			},
			selectit(i) {
				this.ind = i
				this.options_show = false
				this.info.searchType = this.types[this.ind].id
				this.info.month = ''
				this.ind3 = 'all'
				this.showzzc = false
			},

			selectit3(i) {
				this.ind3 = i
				this.options_show3 = false
				this.info.month = this.month[this.ind3]
				this.showzzc = false
			},

			// 点击遮罩层
			close() {
				this.options_show = false
				this.options_show2 = false
				this.options_show3 = false
				this.showzzc = false
			},

		}
	}
</script>
<style scoped>
	.title_num{
		font-weight: bold;
		font-size: 20px;
	}
	.title_top{
		font-size: 16px;
		
	}
	.count{
		text-align: center;
		padding: 20px;
		margin-top: 20px;
		background-color: #fff;
	}
	/deep/ .el-input__icon {
		line-height: 34px;
	}

	/deep/ .el-table .el-table__cell {
		text-align: center;
	}

	/deep/ .el-table .el-table__cell:nth-of-type(1) {
		text-align: left;
	}

	/deep/ .el-table th.el-table__cell>.cell {
		color: #626262;
	}

	/deep/ .el-table .el-table__cell:nth-of-type(2) {
		text-align: left;
		color: #32B16C;
	}

	/deep/ .el-table .el-table__cell:nth-of-type(3) {
		color: #32B16C;
	}

	.table_top {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 23px;
		margin-bottom: 11px;
	}

	.table_title {
		font-size: 16px;
		font-weight: bold;
		color: #626262;
	}

	.top_btns {
		display: flex;
	}

	.top_btns div {
		font-size: 14px;
		color: #FFFFFF;
		text-align: center;
		line-height: 28px;
		border-radius: 3px;
		width: 63px;
		height: 28px;
		margin-left: 13px;
		cursor: pointer;
	}

	.print {
		background: #32B16C
	}

	.export {
		background: #5BC0DE;
	}

	.bg {
		background: #f6f6f6;
		min-height: 100vh;
	}

	.box_top_left {
		padding-top: 17px;
		height: 370px;
		width: 100%;
		background-color: #fff;
	}

	.box_top {
		display: flex;
		justify-content: space-between;
		margin-top: 17px;
	}

	.box {
		-ms-overflow-style: none !important;
	}

	.box::-webkit-scrollbar {
		display: none;
	}

	.box {
		width: 100%;

		overflow-y: scroll;
		height: calc(100vh - 116px);
	}

	/deep/ .el-input--prefix .el-input__inner {
		height: 34px;
		padding: 0 8px;
	}

	.input-cont:hover .clears {
		display: block;
	}

	.clears {
		width: 10px;
		position: absolute;
		right: 35px;
		top: 14px;
		display: none;
		cursor: pointer;
	}

	/deep/ .el-input__inner {
		width: 114px;
		height: 34px;
		/* background: #F6F6F6; */
		border: 1px solid #CCCCCC;
		box-shadow: 0px 2px 0px 0px #EEEEEE;
		border-radius: 5px;
	}
</style>
